<template>
	<div class="system-role-container layout-padding">
		<div class="system-role-padding layout-padding-auto layout-padding-view">
			<div class="system-user-search">
				<el-input size="default" class="w_150 mr10" v-model="listQuery.Name" placeholder="名称" @keydown.enter="searchForm"> </el-input>
				<el-select size="default" v-model="listQuery.IsAuthorization" placeholder="授权状态" class="w_150 mr10" clearable @change="searchForm">
					<el-option label="已授权" :value="'true'" />
					<el-option label="未授权" :value="'false'" />
				</el-select>
				<el-select size="default" v-model="listQuery.AuthenticationMethod" placeholder="鉴权方式" class="w_150 mr10" clearable @change="searchForm">
					<el-option label="IP鉴权" value="1" />
					<el-option label="域名鉴权" value="2" />
				</el-select>
				<!-- <el-select size="default" v-model="listQuery.DepartmentId" placeholder="预警条件" class="w_150 mr10" clearable @change="searchForm">
					<el-option label="是" :value="true" />
					<el-option label="否" :value="false" />
				</el-select>
				<el-select size="default" v-model="listQuery.DepartmentId" placeholder="预警状态" class="w_150 mr10" clearable @change="searchForm">
					<el-option label="限流" value="0" />
					<el-option label="熔断" value="10" />
					<el-option label="拉黑" value="20" />
				</el-select> -->
				<!-- <div class="mt10"> -->
				<el-button size="default" @click="searchForm" type="primary"> 查询 </el-button>
				<el-button size="default" type="success" @click="addRule"> 新增 </el-button>
				<el-button size="default" type="info" @click="resetForm"> 重置 </el-button>
				<el-button size="default" :disabled="!multipleSelection.length" @click="onRowDel(2, '')" type="danger"> 批量删除 </el-button>
				<!-- </div> -->
			</div>
			<tableTem ref="tableRef" v-bind:="state.tableData" @pageChange="onTablePageChange" @onSelectionChange="onSelectionChange">
				<el-table-column type="selection" />
				<el-table-column align="center" type="index" label="序号" width="60" />
				<el-table-column align="center" min-width="120" prop="name" label="名称"></el-table-column>
				<el-table-column align="center" min-width="120" prop="accountNumber" label="授权">
					<template #default="scope">
						<el-tag type="success">{{ scope.row.isAuthorization ? '已' : '未' }}授权</el-tag>
					</template>
				</el-table-column>
				<el-table-column align="center" prop="authorizationTypeNames" min-width="150" label="授权类型">
					<template #default="scope">
						<span>{{ parseJson(scope.row.authorizationTypeNames) }}</span>
					</template>
				</el-table-column>
				<el-table-column align="center" prop="authorizationDate" min-width="150" label="授权期限（天)"></el-table-column>
				<el-table-column align="center" min-width="150" prop="limitQueryDate" label="限制查询(天)"></el-table-column>
				<el-table-column align="center" prop="dutiesName" min-width="120" label="接入接口"></el-table-column>
				<el-table-column align="center" prop="sysRoleName" min-width="120" label="预警条件数量"></el-table-column>
				<el-table-column align="center" min-width="100" prop="status" label="预警状态">
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status == 1">启用</el-tag>
						<el-tag type="info" v-else>冻结</el-tag>
					</template>
				</el-table-column>
				<el-table-column align="center" min-width="200" prop="createTime" label="状态剩余时间 (天)"></el-table-column>
				<el-table-column label="操作" align="center" width="150" fixed="right">
					<template #default="scope">
						<el-button size="small" type="primary" text @click="editOption(scope.row)">编辑</el-button>
						<el-button size="small" type="primary" text @click="getDetail(scope.row)">详情</el-button>
						<el-button size="small" type="primary" text @click="onRowDel(1, scope.row.id)">删除</el-button>
					</template>
				</el-table-column>
			</tableTem>
		</div>
		<!-- 弹窗 -->
		<el-dialog
			:title="state.dialogTitle"
			v-model="state.dialogVisible"
			:close-on-click-modal="false"
			:show-close="false"
			:close-on-press-escape="false"
			width="800px"
		>
			<dialogTem v-if="state.dialogType == 1" :edit-item="state.editItem" :isDisabled="state.isDisabled" @close="closeDialog" />
		</el-dialog>
	</div>
</template>

<script setup lang="ts">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { SubSystemGetPagedList, BatchDelete } from '/@/api/subSystemInfo/index';
// 引入组件
const dialogTem = defineAsyncComponent(() => import('./component/dialog.vue'));
const tableTem = defineAsyncComponent(() => import('/@/components/tables/index.vue'));

// 定义变量内容
const tableRef = ref();
const listQuery = reactive({
	Name: '', //名称
	IsAuthorization: null, // 授权状态
	AuthenticationMethod: '', // 鉴权方式
	DutiesId: '', // 关联职务
});
const state = reactive<any>({
	tableData: {
		data: [],
		total: 0,
		loading: true,
		param: {
			PageIndex: 1,
			PageSize: 10,
		},
	},
	editItem: {},
	isDisabled: false, // 详情
	dialogType: 0, // 弹窗类型
	dialogTitle: '', // 弹窗标题
	dialogVisible: false, // 弹窗状态
});
// 页面加载时
onMounted(() => {
	getTableData();
});

// 初始化表格数据
const getTableData = async () => {
	state.tableData.loading = true;
	try {
		const params = {
			...state.tableData.param,
			...listQuery,
		};
		Object.keys(params).forEach((key) => {
			if (!params[key]) delete params[key];
		});
		let data = await SubSystemGetPagedList(params);
		state.tableData.data = data.data.items;
		state.tableData.total = data.data.totalCount;
	} finally {
		state.tableData.loading = false;
	}
};
const parseJson = (v: any) => {
	if (!v) return;
	if (v.length) {
		return v.join(',');
	} else {
		return v;
	}
};
// 弹窗打开
const openDialog = (title: any, type: any) => {
	state.dialogTitle = title;
	state.dialogType = type;
	state.dialogVisible = true;
};
// 新增
const addRule = () => {
	openDialog('新增', 1);
};
// 编辑
const editOption = (item: any) => {
	state.editItem = item;
	openDialog('编辑', 1);
};
// 详情
const getDetail = (item: any) => {
	state.editItem = item;
	state.isDisabled = true;
	openDialog('详情', 1);
};

// 关闭弹框
const closeDialog = () => {
	state.dialogTitle = '';
	state.dialogType = 0;
	state.dialogVisible = false;
	state.editItem = {};
	state.isDisabled = false;
	searchForm();
};
// 多选数据
const multipleSelection = ref([]);
// 多选
const onSelectionChange = (val: any) => {
	multipleSelection.value = val;
};

// 删除账户
const onRowDel = (type: any, id: any) => {
	ElMessageBox.confirm(`此操作将永久子系统信息，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(async () => {
		const ids = [] as any;

		if (type == 1) {
			ids.push(id);
		} else {
			if (multipleSelection.value.length === 0) {
				ElMessage({
					type: 'warning',
					message: '请选择要删除的数据',
				});
				return;
			}
			multipleSelection.value &&
				multipleSelection.value.map((item: any) => {
					ids.push(item.id);
				});
		}

		await BatchDelete(ids);
		ElMessage.success('删除成功');
		getTableData();
	});
};
// 分页改变
const onTablePageChange = (val: any) => {
	state.tableData.param.PageIndex = val.pageNum;
	state.tableData.param.PageSize = val.pageSize;
	getTableData();
};
// 搜索点击时表单回调
const searchForm = () => {
	state.tableData.param.PageIndex = 1;
	tableRef.value.pageReset();
};
// 重置
const resetForm = () => {
	listQuery.Name = '';
	listQuery.IsAuthorization = null;
	listQuery.AuthenticationMethod = '';
	searchForm();
};
</script>

<style scoped lang="scss">
.system-role-container {
	.system-role-padding {
		padding: 15px;
		.el-table {
			flex: 1;
		}
	}
}
</style>
